﻿using System;
using System.IO;
using System.Threading.Tasks;

public class FileCopyManager
{
    public static void CopyDirectory(string sourceDir, string destDir)
    {
        if (!Directory.Exists(destDir))
        {
            Directory.CreateDirectory(destDir);
        }

        string[] files = Directory.GetFiles(sourceDir);
        string[] subDirs = Directory.GetDirectories(sourceDir);

        Parallel.ForEach(files, file =>
        {
            string fileName = Path.GetFileName(file);
            string destPath = Path.Combine(destDir, fileName);
            CopyFileWithBuffer(file, destPath);
        });

        foreach (string subDir in subDirs)
        {
            string subDirName = Path.GetFileName(subDir);
            string destSubDir = Path.Combine(destDir, subDirName);
            CopyDirectory(subDir, destSubDir);
        }
    }

    private static void CopyFileWithBuffer(string source, string destination)
    {
        const int bufferSize = 1024 * 1024;
        using (FileStream sourceStream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read))
        using (FileStream destStream = new FileStream(destination, FileMode.Create, FileAccess.Write))
        {
            byte[] buffer = new byte[bufferSize];
            int bytesRead;
            while ((bytesRead = sourceStream.Read(buffer, 0, bufferSize)) > 0)
            {
                destStream.Write(buffer, 0, bytesRead);
            }
        }
        Console.WriteLine($"复制完成: {source} -> {destination}");
    }
}

public class FileCopyTest
{
    public static void Main(string[] args)
    {
        Console.WriteLine("请输入源文件夹路径:");
        string sourceDirectory = Console.ReadLine();

        if (!Directory.Exists(sourceDirectory))
        {
            Console.WriteLine("错误：源文件夹不存在，请重新运行程序并输入正确路径！");
            Console.ReadLine();
            return;
        }

        Console.WriteLine("请输入目标文件夹路径:");
        string destinationDirectory = Console.ReadLine();

        Console.WriteLine("开始复制文件夹...");
        DateTime startTime = DateTime.Now;

        FileCopyManager.CopyDirectory(sourceDirectory, destinationDirectory);

        TimeSpan elapsed = DateTime.Now - startTime;
        Console.WriteLine($"复制完成，耗时: {elapsed.TotalSeconds:F2}秒");
        Console.ReadLine();
    }
}